/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/15/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfprojectpilot.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.application.yfprojectpilot.entity.YfProjectPilot;
import com.application.yfprojectpilot.struts.form.YfProjectPilotForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:YfProjectPilotDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/15/2012
 * 
 */
public class YfProjectPilotDAO extends HibernateGenericDao<YfProjectPilot> {
	private static final Logger logger = Logger
			.getLogger(YfProjectPilotDAO.class);

	private YfProjectPilot yfProjectPilot;

	public YfProjectPilot getYfProjectPilot() {
		return this.yfProjectPilot;
	}

	public void setYfProjectPilot(YfProjectPilot yfProjectPilot) {
		this.yfProjectPilot = yfProjectPilot;
	}

	public Integer getAllCountYfProjectPilot(
			YfProjectPilotForm yfProjectPilotForm) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT COUNT(0) FROM yf_project_pilot t left join yf_project p on t.project_no = p.project_no left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1");
		sql = getStringBuffer(yfProjectPilotForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}

	public void deleteYfProjectPilots(YfProjectPilotForm yfProjectPilotForm) {
		String hql = " delete from YfProjectPilot where id = '"+ yfProjectPilotForm.getId() + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfProjectPilots failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfProjectPilot(YfProjectPilotForm yfProjectPilotForm,
			PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT t.*,u.rname rname,p.*,b.name project_org_name  FROM yf_project_pilot t left join yf_project p on t.project_no = p.project_no left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfProjectPilotForm, sql, args);
		sql.append(" order by t.create_date desc ");
		return getPageInfo(pageBean, sql.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(YfProjectPilotForm yfProjectPilotForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfProjectPilotForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfProjectPilotForm.getProjectNo().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfProjectPilotForm.getPilotName())) {
			buf.append(" and t.PILOT_NAME like ? ");
			args.add("%" + yfProjectPilotForm.getPilotName().trim() + "%");
		}

		if (null != yfProjectPilotForm.getTestType()
				&& yfProjectPilotForm.getTestType().longValue() != 0) {
			buf.append(" and t.TEST_TYPE = ? ");
			args.add(yfProjectPilotForm.getTestType());
		}

		if (null != yfProjectPilotForm.getAuditStatus()
				&& yfProjectPilotForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfProjectPilotForm.getAuditStatus());
		}
		if (StringUtils.isNotBlank(yfProjectPilotForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfProjectPilotForm.getId());
		}
		if (StringUtils.isNotBlank(yfProjectPilotForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfProjectPilotForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfProjectPilotForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfProjectPilotForm.getProjectOrg());
		}
		if (null != yfProjectPilotForm.getProjectType()
				&& yfProjectPilotForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfProjectPilotForm.getProjectType());
		}
		if (StringUtils.isBlank(yfProjectPilotForm.getId())) {
			buf = CommonUtil.filterData(buf,args);
		}
		
		return buf;
	}

}
